class CountryrController < ApplicationController
  include AuthenticatedSystem
  # GET /countryr
  # GET /countryr.xml
  def index 
    @liste_continent = Continent.find :all, :order=>"name asc"
    @liste_pays = Country.find :all, :order=>"name asc"
    
    @country = Country.find :all
  end
  
  #---------------------------------------------------------------------------------------------------------
  # Action lors d'une recherche (Textuelle ou par pays)
  #---------------------------------------------------------------------------------------------------------
  def search 
    # Utiliser un filtre sur la recherche textuelle
    if params[:fieldSearch]!=""
      @albums = Album.find_by_sql("SELECT * FROM albums WHERE title LIKE '%#{params[:fieldSearch]}%' OR description LIKE '%#{params[:fieldSearch]}%'")
    else
      @albums = Album.find(:all, :conditions => ["country_id = :cId", {:cId => params[:countries][:country_id]}]) 
    end
  end
  
  #---------------------------------------------------------------------------------------------------------
  # Affiche les albums du continent Europe
  #---------------------------------------------------------------------------------------------------------
  def europe
    @lesAlbums = Album.find(:all)
    @albums = []
    
    for album in @lesAlbums
      if album.country.continent.name == "Europe"
        @albums = @albums + [album]
      end
    end
    
    render :action => "search"
  end
  
  #---------------------------------------------------------------------------------------------------------
  # Affiche les albums du continent Afrique
  #---------------------------------------------------------------------------------------------------------
  def afrique
    @lesAlbums = Album.find(:all)
    @albums = []
    
    for album in @lesAlbums
      if album.country.continent.name == "Afrique"
        @albums = @albums + [album]
      end
    end
    
    render :action => "search"
  end
  
  #---------------------------------------------------------------------------------------------------------
  # Affiche les albums du continent Asie
  #---------------------------------------------------------------------------------------------------------
  def asie
    @lesAlbums = Album.find(:all)
    @albums = []
    
    for album in @lesAlbums
      if album.country.continent.name == "Asie"
        @albums = @albums + [album]
      end
    end
    
    render :action => "search"
  end
  
  #---------------------------------------------------------------------------------------------------------
  # Affiche les albums du continent Océanie
  #---------------------------------------------------------------------------------------------------------
  def oceanie
    @lesAlbums = Album.find(:all)
    @albums = []
    
    for album in @lesAlbums
      if album.country.continent.name == "Océanie"
        @albums = @albums + [album]
      end
    end
    
    render :action => "search"
  end
  
  #---------------------------------------------------------------------------------------------------------
  # Affiche les albums du continent Amérique du Nord
  #---------------------------------------------------------------------------------------------------------
  def ameriquedunord
    @lesAlbums = Album.find(:all)
    @albums = []
    
    for album in @lesAlbums
      if album.country.continent.name == "Amérique du Nord"
        @albums = @albums + [album]
      end
    end
    
    render :action => "search"
  end
 
  #---------------------------------------------------------------------------------------------------------
  # Affiche les albums du continent  Amérique du Sud
  #---------------------------------------------------------------------------------------------------------
  def ameriquedusud
    @lesAlbums = Album.find(:all)
    @albums = []
    
    for album in @lesAlbums
      if album.country.continent.name == "Amérique du Sud"
        @albums = @albums + [album]
      end
    end
    
    render :action => "search"
  end
  
  #---------------------------------------------------------------------------------------------------------
  # UPDATELIST - met à jour la liste des pays en fonction du continent choisi
  #---------------------------------------------------------------------------------------------------------
  def updatelist
    choix_continent = params['id_continent']
    @liste_pays = Country.find_all_by_continent_id(choix_continent, :order => "name desc")
  end
end
